package com.itheima.controller;


import com.google.gson.internal.$Gson$Preconditions;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBen;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/checkitem")
public class CheckItemController {
    @Autowired
    private CheckItemService checkItemService;

    @RequestMapping("/findAll")
    public Result findAll(){

       List<CheckItem> checkItems= checkItemService.findAll();
        return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,checkItems);
    }
    @RequestMapping("/add")
    public Result add(@RequestBody CheckItem checkItem){
      checkItemService.add(checkItem);
        return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS);
    }

    /**
     * 分页查询
     * @param queryPageBen
     * @return
     */
    @RequestMapping("/findPage")
    public Result findPAge(@RequestBody QueryPageBen queryPageBen){
      PageResult<CheckItem> pageResult= checkItemService.findPage(queryPageBen);
        return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,pageResult);

    }

    /**
     * 删除检查项
     * @param id
     * @return
     */
   @DeleteMapping("/{id}")
    public Result deleteByid(@PathVariable int id){
        checkItemService.deleteByid(id);
        return new Result(true, MessageConstant.DELETE_CHECKITEM_SUCCESS );
    }

    /**
     * 编辑回显
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result findById(@PathVariable int id){
        CheckItem checkItem=checkItemService.findById(id);
        return new Result(true, MessageConstant.EDIT_CHECKITEM_FAIL,checkItem);
    }

    @PutMapping("/{id}")
    public Result update(@PathVariable int id ,@RequestBody CheckItem checkItem ){
        checkItem.setId(id);
       checkItemService.upDateByID(checkItem);
        return new Result(true, MessageConstant.EDIT_CHECKITEM_SUCCESS);
    }

}
